import { getChannels, getBodys } from '../../api/home'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    channels: [],
    active: 0,
    refresher: false,
    channel_id: '',
    with_top: 1,
    timestamp: '',
    results:[],
    isLoading:true,
    isFinish:false,
    id:''

  },
  async onClick (e) {
    this.setData({
      channel_id: e.detail.name
    })
   this.getList()
  },
  
   async getList(){
    this.getItems()

    const obj = {
      channel_id: this.data.channel_id,
      with_top: 1,
      timestamp: this.data.timestamp
    }
    const res = await getBodys(obj)
    // console.log(res);
    this.setData({
      results:[...res.data.results,...this.data.results]
    })
  },

  getItems () {
    const items = +new Date()
    this.setData({
      timestamp: items
    })
  },
  onPulling () {
    setTimeout(() => {
      this.setData({
        channel_id: this.data.channel_id,
        results:[],
        refresher:false
      })
      this.getList()

    }, 2000)
    
    // 重新发起数据请求
  },
// 去详情页面
toDeails(e){
  console.log(e.currentTarget.dataset.id);
  this.setData({
    id:e.currentTarget.dataset.id
  })
  wx.navigateTo({
    url: `/pages/details/details?id=${this.data.id}`

  })
},
  clickSearch () {
    wx.navigateTo({
      url: "/pages/search/search"
    })
  },
  dropdown () {
    console.log('dropdown')
  },
  async getChannels () {
    const res = await getChannels()
    this.setData({ channels: res.data.channels })
    console.log(res);
    this.selectComponent('#tabs').resize();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.getChannels()
    this.setData({
      channel_id: 1
    })
    this.getList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    console.log('111');
 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
 onReachBottom () {
  console.log('111');
this.getList()
  },
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})